/**
 * @author Nicolas Latorre (nicolas.latorre@usi.ch)
 */

package ch.usi.inf.pf2.file.interaction;



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Scanner;

import ch.usi.inf.pf2.circuit.Component;

public final class InstructionReader {
	private String text;
	private String fileLocation;
	private ArrayList<Component> circuit;
	
	public InstructionReader(String location) {
		fileLocation = location;
		circuit = new ArrayList<Component>();
		readInstruction(fileLocation);
	}
	
	public void readInstruction(final String fileLocation) {
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(fileLocation);
			in = new ObjectInputStream(fis);
			
			circuit = (ArrayList<Component>)in.readObject();
			in.close();
		}
		catch(FileNotFoundException e) {
			System.err.println("Unable to open " + fileLocation);
		}
		catch(IOException e) {
			System.err.println("A problem was encoutered reading " + fileLocation);
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		for(Component c : circuit) {
			System.out.println(c);
		}
	}
	
	public ArrayList<Component> getCircuit() {
		return circuit;
	}
	
	
	
//	//This variable contains the location of the file.
//	private String fileLocation;
//	
//	private String text;
//	
//	public InstructionReader(String location) {
//		fileLocation = location;
//		readInstruction(fileLocation);
//	}
//	
//	/**
//	 * This method read a file that is passed as parameter and store the instruction in the ArrayList instructions.
//	 * @param fileLocation, this string contains the path of the file.
//	 */
//	public void readInstruction(final String fileLocation) {
//		try {
//			BufferedReader reader = new BufferedReader(new FileReader(fileLocation));
//			String instructionLine = reader.readLine();
//			String tempText = "";
//			while(instructionLine != null) {
//				//instructions.add(instructionLine);
//				instructionLine = reader.readLine();
//				tempText = tempText + instructionLine;
//			}
//			text = tempText.replaceAll("\t+", "");
//			text = text.replaceAll("</[a-z]+>", "");
//			System.out.println(text);
//			reader.close();
//		}
//		catch(FileNotFoundException e) {
//			System.err.println("Unable to open " + fileLocation);
//		}
//		catch(IOException e) {
//			System.err.println("A problem was encoutered reading " + fileLocation);
//		}
//	}
//	
//	/**
//	 * This method ask to the user to select the location of the file in the computer.
//	 * @return inputLine, this string contains the path of the file.
//	 */
//	public void insertLocationOfTheFile() {
//		System.out.println("Where is the file that you want to read?");
//		Scanner location = new Scanner(System.in);
//		String inputLine = location.nextLine();
//		fileLocation = inputLine;
//	}
//	
//	public String getLocation() {
//		return fileLocation;
//	}
//	
	public String getText() {
		return text;
	}
}
